package com.event.system.service.impl;

import com.event.common.constant.UserConstants;
import com.event.common.core.text.Convert;
import com.event.common.utils.StringUtils;
import com.event.system.domain.SysEventRoster;
import com.event.system.domain.SysEventSort;
import com.event.system.mapper.SysEventOrderMapper;
import com.event.system.mapper.SysEventRegionMapper;
import com.event.system.mapper.SysEventRosterMapper;
import com.event.system.service.ISysEventRosterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysEventRosterServiceImpl implements ISysEventRosterService {

    @Autowired
    private SysEventRosterMapper mapper;

    @Autowired
    private SysEventRegionMapper sysEventRegionMapper;

    @Autowired
    private SysEventOrderMapper sysEventOrderMapper;

    /**
     * 根据条件分页查询展会名单信息
     *
     * @param sysEventRoster
     * @return
     */
    @Override
    public List<SysEventRoster> selectSysEventRosterList(SysEventRoster sysEventRoster) {
        return mapper.selectSysEventRosterList(sysEventRoster);
    }

    /**
     * 这里是模糊查询，通过字符串模糊查询
     *
     * @param sortName
     * @return
     */
    @Override
    public List<SysEventRoster> selectSortEventRosterListByName(String sortName) {
        return mapper.selectSysEventRosterByEventName(sortName);
    }

    /**
     * 根据id查找名单信息
     *
     * @param sortId
     * @return
     */
    @Override
    public SysEventRoster selectSortEventRosterBySortId(Long sortId) {
        return mapper.selectSysEventRosterByRosterId(sortId);
    }

    /**
     * 新增展会名单信息
     *
     * @param sysEventRoster
     * @return
     */
    @Override
    public int add(SysEventRoster sysEventRoster) {
        return mapper.add(sysEventRoster);
    }

    /**
     * 修改展会名单信息
     *
     * @param sysEventRoster
     * @return
     */
    @Override
    public int update(SysEventRoster sysEventRoster) {
        return mapper.update(sysEventRoster);
    }

    /**
     * 删除展会名单信息
     *
     * @param sortIds
     * @return
     */
    @Override
    @Transactional
    public int deleteSysEventRosterBySortId(String sortIds) {
        Long[] ids = Convert.toLongArray(sortIds);
        for (int i = 0; i < ids.length; i++) {
            System.out.println("【测试】：" + ids[i]);
            List<Long> longs = sysEventOrderMapper.selectOrderIdByRosterId(ids[i]);
            System.out.println("【List<Long>】：" + longs.size());
            if (longs != null && longs.size() != 0){
                sysEventOrderMapper.deleteSysEventOrder(longs);
            }

        }
        sysEventRegionMapper.deleteSysEventRegionByRosterId(ids);
        return mapper.deleteSysEventRosterByRosterId(ids);
    }

    /**
     * 校验名单名称是否唯一
     *
     * @param sysEventRoster
     * @return
     */
    @Override
    public String checkEventNameUnique(SysEventRoster sysEventRoster) {
        Long rosterId = StringUtils.isNull(sysEventRoster.getRosterId()) ? -1L : sysEventRoster.getRosterId();
        SysEventRoster sysEventRoster1 = mapper.checkEventNameUnique(sysEventRoster.getEventName());
        if (StringUtils.isNotNull(sysEventRoster1) && sysEventRoster1.getRosterId().longValue() != rosterId.longValue())
        {
            return UserConstants.SORT_NAME_NOT_UNIQUE;
        }
        return UserConstants.SORT_NAME_UNIQUE;
    }

    /**
     * 查询展会名单信息，用来做列表
     *
     * @return
     */
    @Override
    public List<SysEventRoster> selectSysEventRosterByAll() {
        return mapper.selectSysEventRosterByAll();
    }

    /**
     * 根据分类ID查询名单
     *
     * @param sortId
     * @return
     */
    @Override
    public List<SysEventRoster> selectSysEventRosterBysortId(Long sortId) {
        return mapper.selectSysEventRosterBysortId(sortId);
    }
}
